package com.example.casper.Experiment2024;
import static android.widget.Toast.LENGTH_LONG;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import android.widget.Button;
import com.example.casper.Experiment2024.data.Shop;
import com.example.casper.Experiment2024.data.ShopDownLoader;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.TextureMapView;
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptorFactory;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
import com.tencent.tencentmap.mapsdk.maps.model.Marker;
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;
import java.util.ArrayList;

public class TencentMapFragment extends Fragment {
    // TODO: Rename and change types of parameters
    private TextureMapView mapView;
    public TencentMapFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tencent_map, container, false);
        mapView = rootView.findViewById(R.id.mapview_tencent);
        TencentMap tencentMap = mapView.getMap();


        // 检查tencentMap是否为空
        if (tencentMap == null) {
            throw new IllegalStateException("TencentMap is not initialized.");
        }
        Button zoomInButton = rootView.findViewById(R.id.zoomInButton);
        Button zoomOutButton = rootView.findViewById(R.id.zoomOutButton);
        zoomInButton.setOnClickListener(v -> tencentMap.animateCamera(CameraUpdateFactory.zoomIn()));

        zoomOutButton.setOnClickListener(v -> tencentMap.animateCamera(CameraUpdateFactory.zoomOut()));

        LatLng point1 = new LatLng(22.249540,113.534584);
        tencentMap.getUiSettings().setZoomGesturesEnabled(true);
        tencentMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point1,19));
        MarkerOptions markerOptions = new MarkerOptions()
                .position(point1)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.qiqiu)) // 设置 Marker 的图像资源
                .title("暨南大学珠海校区")
                .snippet("这是我们的学校");
        Marker marker = tencentMap.addMarker(markerOptions);

        new Thread(() -> {
            ShopDownLoader shopdataloader=new ShopDownLoader();
            String jsonFileContent= shopdataloader.DownLoad("https://file.notion.so/f/f/3500cdb4-2af2-4803-b797-b49933980410/8332c677-d2bd-461f-97da-5a84839038f4/shop.json?table=block&id=143148a1-26c1-8090-acdf-d425c3687691&spaceId=3500cdb4-2af2-4803-b797-b49933980410&expirationTimestamp=1734235200000&signature=mHp-mX4o41B53tQ0NYL0Ae1PV7pQngZNBkZjySJBQeA&downloadName=shop.json");
            ArrayList<Shop> locations= shopdataloader.parsonJson(jsonFileContent);
            if(null==TencentMapFragment.this.getActivity())return;
            TencentMapFragment.this.getActivity().runOnUiThread(() -> {
                for (Shop shop: locations
                ) {
                    LatLng point11 = new LatLng(shop.getLatitude(), shop.getLongitude());
                    // 创建 MarkerOptions
                    MarkerOptions markerOptions1 = new MarkerOptions()
                            .position(point11)
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.qiqiu))
                            .title(shop.getName())
                            .snippet(shop.getMemo());

                    Marker marker1 = tencentMap.addMarker(markerOptions1);
                    marker1.setClickable(true);
                }
            });
        }).start();

        tencentMap.setOnMarkerClickListener(marker12 -> {
            if(marker12.getId().equals(marker12.getId())) {
                Toast.makeText(TencentMapFragment.this.getContext(),"Your clicked tencent map", LENGTH_LONG).show();
            }
            return true;
        });
        marker.setClickable(true);
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        mapView.onStart();
    }
    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }
    @Override
    public void onStop() {
        super.onStop();
        mapView.onStop();
    }
    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

}